package com.shanghaionstar.notification.service;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;

import com.shanghaionstar.R;
import com.shanghaionstar.notification.bean.NotificationConstants;
import com.shanghaionstar.notification.bean.XMPPMessage;

import java.util.Random;

/**
 * 推送广播接收
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class MessageReceiver extends BroadcastReceiver {

    private String link;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(NotificationConstants.RECEIVER_NAME)) {
            XMPPMessage msg = (XMPPMessage) intent.getExtras().getSerializable("message");
            if (msg == null)
                return;
            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            //设置通知的头信息
            builder.setContentTitle(context.getString(R.string.app_name));
            //设置通知的内容信息
            builder.setContentText(msg.getContent()).setColor(Color.BLACK);
            //设置通知的icon
            builder.setSmallIcon(R.drawable.onstar_icon);
            //设置通知的提示信息
            builder.setTicker(context.getString(R.string.app_name));
            builder.setAutoCancel(true);
            //设置提醒方式
            builder.setDefaults(Notification.DEFAULT_ALL);
            //设置震动的范式
            builder.setVibrate(new long[]{0, 300, 500, 700});

            //判断是否是长文本
            if (!TextUtils.isEmpty(msg.getReceiveType()) && msg.getReceiveType().equalsIgnoreCase(NotificationConstants.B2C_RECEIVETYPE_REMOTE)) {
                NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
                bigTextStyle.bigText(msg.getContent());
                builder.setStyle(bigTextStyle);
            }

            Intent targetIntent = new Intent();
            targetIntent.putExtra("receiveType", msg.getReceiveType());
            //如果是link，则跳转网页
            if (!TextUtils.isEmpty(msg.getReceiveType()) && (msg.getReceiveType().equalsIgnoreCase(NotificationConstants.B2C_RECEIVETYPE_URL)||msg.getReceiveType().equalsIgnoreCase(NotificationConstants.B2C_RECEIVETYPE_DRIVINGSCORE))) {
//				link = msg.getLink();
                targetIntent.putExtra("uri", msg.getLink());
            }
            targetIntent.putExtra("title", msg.getTitle());
            targetIntent.setComponent(new ComponentName("com.shanghaionstar", "com.shanghaionstar.activity.LoadingActivity"));
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, targetIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(pendingIntent);

            Random random = new Random();
//            LogManager.getInstance().d("------------------message receiver received message-----------------" + ApplicationUtil.frontActivityCount(context));
//            if (!ApplicationUtil.isActivityFront(context)) {
            manager.notify(random.nextInt(), builder.build());
//            }
        } else if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
//			NotificationControl.getInstants(context).startNotification();
        }
    }
}
